#include "mpr.h"
#include "rectype.h"
#include "util.h"
#include "nmfield.h"

#include "nmarrfield.h"
#include "nmarrfield.cpp"

#include "cnfield.h"

#include "stdffield.h"
#include "stdffield.cpp"

using namespace std;
using namespace opsrc::stdf;

Mpr::Mpr()
{}

Mpr::Mpr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf )
    :RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf != 0)
        readFields(buf);
}

Mpr::~Mpr() {
}


const StdfField<long> Mpr::getTestNumber() const {
    return test_num;
}

void Mpr::setTestNumber(StdfField<long> testnum) {
    test_num = testnum;
}

const StdfField<int> Mpr::getHeadNumber() const {
    return head_num;
}

void Mpr::setHeadNumber(StdfField<int> headnum) {
    head_num = headnum;
}

const StdfField<int> Mpr::getSiteNumber() const {
    return site_num;
}

void Mpr::setSiteNumber(StdfField<int> sitenum) {
    site_num = sitenum;
}

const StdfField<vector<bool> > Mpr::getTestFlag() const {
    return test_flag;
}

void Mpr::setTestFlag(StdfField<vector<bool> > testflag) {
    test_flag = testflag;
}

const StdfField<vector<bool> > Mpr::getParameterFlag() const {
    return parm_flag;
}

void Mpr::setParameterFlag(StdfField<vector<bool> > parmflag) {
    parm_flag = parmflag;
}

const StdfField<int> Mpr::getPmrCountIndexes() const {
    return rtn_icnt;
}

void Mpr::setPmrCountIndexes(StdfField<int> rtnicnt) {
    rtn_icnt = rtnicnt;
}

const StdfField<int> Mpr::getReturnResultsCount() const {
    return rslt_cnt;
}

void Mpr::setReturnResultCount(StdfField<int> rsltcnt) {
    rslt_cnt = rsltcnt;
}			

const StdfField<vector<int> > Mpr::getReturnStatesArray() const {
    return rtn_stat;
}

void Mpr::setReturnStatesArray(StdfField<vector<int> > rtnstat) {
    rtn_stat = rtnstat;
}


const StdfField<vector<float> > Mpr::getReturnResultsArray() const {
    return rtn_rslt;
}

void Mpr::setReturnResultsArray(StdfField<vector<float> > rtnrslt) {
    rtn_rslt = rtnrslt;
}

const StdfField<string> Mpr::getTestDescriptiveText() const {
    return test_txt;
}
void Mpr::setTestDescriptiveText(StdfField<string> testtxt) {
    test_txt = testtxt;
}

const StdfField<string> Mpr::getAlarmId() const {
    return alrm_id;
}

void Mpr::setAlarmId(StdfField<string> alrmid) {
    alrm_id = alrmid;
}

const StdfField<vector<bool> > Mpr::getOptionFlag() const {
    return opt_flag;
}

void Mpr::setOptionFlag(StdfField<vector<bool> > optflag) {
    opt_flag = optflag;
}			

const StdfField<int> Mpr::getTestResultScale() const {
    return res_scal;
}

void Mpr::setTestResultScale(StdfField<int> resscal) {
    res_scal = resscal;
}

const StdfField<int> Mpr::getTestLowerLimitScale() const {
    return llm_scal;
}

void Mpr::setTestLowerLimitScale(StdfField<int> llmscal) {
    llm_scal = llmscal;
}

const StdfField<int> Mpr::getTestUpperLimitScale() const {
    return hlm_scal;
}

void Mpr::setTestUpperLimitScale(StdfField<int> hlmscal) {
    hlm_scal = hlmscal;
}

const StdfField<float> Mpr::getTestLowerLimitValue() const {
    return lo_limit;
}

void Mpr::setTestLowerLimitValue(StdfField<float> lolimit) {
    lo_limit = lolimit;
}

const StdfField<float> Mpr::getTestUpperLimitValue() const {
    return hi_limit;
}

void Mpr::setTestUpperLimitValue(StdfField<float> hilimit) {
    hi_limit = hilimit;
}

const StdfField<float> Mpr::getStartingInValue() const {
    return start_in;
}

void Mpr::setStartingInValue(StdfField<float> startin) {
    start_in = startin;
}

const StdfField<float> Mpr::getStartingIncrementalValue() const {
    return incr_in;
}

void Mpr::setStartingIncrementalValue(StdfField<float> incrin) {
    incr_in = incrin;
}

const StdfField<vector<unsigned int> > Mpr::getPmrIndexArray() const {
    return rtn_indx;
}

void Mpr::setPmrIndexArray(StdfField<vector<unsigned int> > rtnindx) {
    rtn_indx = rtnindx;
}

const StdfField<string> Mpr::getUnits() const {
    return units;
}

void Mpr::setUnits(StdfField<string> _units) {
    units = _units;
}

const StdfField<string> Mpr::getInputConditionUnits() const {
    return units_in;
}

void Mpr::setInputConditionUnits(StdfField<string> unitsin) {
    units_in = unitsin;
}

const StdfField<string> Mpr::getResultFormat() const {
    return c_resfmt;
}

void Mpr::setResultFormat(StdfField<string> resfmt) {
    c_resfmt = resfmt;
}

const StdfField<string> Mpr::getLowerLimitFormat() const {
    return c_llmfmt;
}

void Mpr::setLowerLimitFormat(StdfField<string> llmfmt) {
    c_llmfmt = llmfmt;
}

const StdfField<string> Mpr::getUpperLimitFormat() const {
    return c_hlmfmt;
}

void Mpr::setUpperLimitFormat(StdfField<string> hlmfmt) {
    c_hlmfmt = hlmfmt;
}

const StdfField<float> Mpr::getLowerSpecLimit() const {
    return lo_spec;
}

void Mpr::setLowerSpecLimit(StdfField<float> lospec) {
    lo_spec = lospec;
}

const StdfField<float> Mpr::getUpperSpecLimit() const {
    return hi_spec;
}

void Mpr::setUpperSpecLimit(StdfField<float> hispec) {
    hi_spec = hispec;
}

void Mpr::readFields(char *buf) {

#if _DEBUG
    cout << "stdfrec= MPR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read test number , 4 byte
    NmField<long, 4> testnum(buf+curpos, en);
    test_num.set(testnum.getData(), curpos, testnum.getByteCount(), testnum.getTotalByteCount());
    curpos += test_num.getTotalByteCount();

    // Read Head num, 1 byte
    if (curpos < reclen -1){
        NmField<unsigned int, 1> headnum(buf+curpos, en);
        head_num.set(headnum.getData(), curpos, headnum.getByteCount(), headnum.getTotalByteCount());
        curpos += head_num.getTotalByteCount();
    }

    // Read Site num, 1 byte
    if (curpos < reclen -1){
        NmField<unsigned int, 1> sitenum(buf+curpos, en);
        site_num.set(sitenum.getData(), curpos, sitenum.getByteCount(), sitenum.getTotalByteCount());
        curpos += site_num.getTotalByteCount();
    }


    // Read test flag
    if (curpos < reclen -1){
        NmArrField<bool, 1 > testflag(buf+curpos, en,1 );
        test_flag.set(testflag.getData(), curpos, testflag.getByteCount(), testflag.getTotalByteCount());
        curpos += test_flag.getTotalByteCount();
    }

    // Read parameter flag
    if (curpos < reclen -1){
        NmArrField<bool, 1 > parmflag(buf+curpos, en,1 );
        parm_flag.set(parmflag.getData(), curpos, parmflag.getByteCount(), parmflag.getTotalByteCount());
        curpos += parm_flag.getTotalByteCount();
    }

    // Read PMR count of return indexes
    if (curpos < reclen -1){
        NmField<unsigned int, 2 > rtnicnt(buf+curpos, en);
        rtn_icnt.set(rtnicnt.getData(), curpos, rtnicnt.getByteCount(), rtnicnt.getTotalByteCount());
        curpos += rtn_icnt.getTotalByteCount();
    }

    // Read returned result  count
    if (curpos < reclen -1){
        NmField<unsigned int, 2 > rsltcnt(buf+curpos, en);
        rslt_cnt.set(rsltcnt.getData(), curpos, rsltcnt.getByteCount(), rsltcnt.getTotalByteCount());
        curpos += rslt_cnt.getTotalByteCount();
    }

    // Read Array of returned result states
    if (curpos < reclen -1){
        NmArrField<Nibble, 1 > rtnstat(buf+curpos, en, rslt_cnt.getData());
        rtn_stat.set(rtnstat.getData(), curpos, rtnstat.getByteCount(), rtnstat.getTotalByteCount());
        curpos += rtn_stat.getTotalByteCount();
    }

    // Read Array of returned result states
    if (curpos < reclen -1){
        NmArrField<float, 4 > rtnrslt(buf+curpos, en, rslt_cnt.getData() );
        rtn_rslt.set(rtnrslt.getData(), curpos, rtnrslt.getByteCount(), rtnrslt.getTotalByteCount());
        curpos += rtn_rslt.getTotalByteCount();
    }

    // Read test description text
    if (curpos < reclen -1){
        CnField<string> testtxt(buf+curpos, en);
        test_txt.set(testtxt.getData(), curpos, testtxt.getByteCount(), testtxt.getTotalByteCount());
        curpos += test_txt.getTotalByteCount();
    }

    // Read alarm id
    if (curpos < reclen -1){
        CnField<string> alrmid(buf+curpos, en);
        alrm_id.set(alrmid.getData(), curpos, alrmid.getByteCount(), alrmid.getTotalByteCount());
        curpos += alrm_id.getTotalByteCount();
    }

    //updated by Jimmy @ 2010-04-09
    // Read test flag
    if (curpos < reclen -1){
        NmArrField<bool, 1 > optflag(buf+curpos,en, 1);
        opt_flag.set(optflag.getData(), curpos, optflag.getByteCount(), optflag.getTotalByteCount());
        curpos += opt_flag.getTotalByteCount();
    }

    // Read result scale
    if (curpos < reclen -1){
        NmField<short, 1 > resscal(buf+curpos, en);
        res_scal.set(resscal.getData(), curpos, resscal.getByteCount(), resscal.getTotalByteCount());
        curpos += res_scal.getTotalByteCount();
    }

    // Read low limit scale exponent
    if (curpos < reclen -1){
        NmField<short, 1 > llmscal(buf+curpos, en);
        llm_scal.set(llmscal.getData(), curpos, llmscal.getByteCount(), llmscal.getTotalByteCount());
        curpos += llm_scal.getTotalByteCount();
    }

    // Read high limit scale exponent
    if (curpos < reclen -1){
        NmField<short, 1 > hlmscal(buf+curpos, en);
        hlm_scal.set(hlmscal.getData(), curpos, hlmscal.getByteCount(), hlmscal.getTotalByteCount());
        curpos += hlm_scal.getTotalByteCount();
    }

    // Read test low value
    if (curpos < reclen -1){
        NmField<float, 4 > lolimit(buf+curpos, en);
        lo_limit.set(lolimit.getData(), curpos, lolimit.getByteCount(), lolimit.getTotalByteCount());
        curpos += lo_limit.getTotalByteCount();
    }

    // Read test high value
    if (curpos < reclen -1){
        NmField<float, 4 > hilimit(buf+curpos, en);
        hi_limit.set(hilimit.getData(), curpos, hilimit.getByteCount(), hilimit.getTotalByteCount());
        curpos += hi_limit.getTotalByteCount();
    }

    // Read test start value input
    if (curpos < reclen -1){
        NmField<float, 4 > startin(buf+curpos, en);
        start_in.set(startin.getData(), curpos, startin.getByteCount(), startin.getTotalByteCount());
        curpos += start_in.getTotalByteCount();
    }

    // Read test start value input increment
    if (curpos < reclen -1){
        NmField<float, 4 > incrin(buf+curpos, en);
        incr_in.set(incrin.getData(), curpos, incrin.getByteCount(), incrin.getTotalByteCount());
        curpos += incr_in.getTotalByteCount();
    }

    // Array of returned indexes
    if (curpos < reclen -1){
        NmArrField<unsigned int, 2 > rtnindx(buf+curpos, en, rtn_icnt.getData() );
        rtn_indx.set(rtnindx.getData(), curpos, rtnindx.getByteCount(), rtnindx.getTotalByteCount());
        curpos += rtn_indx.getTotalByteCount();
    }

    // Units of returned results
    if (curpos < reclen -1){
        CnField<string> t_units(buf+curpos, en);
        units.set(t_units.getData(), curpos, t_units.getByteCount(), t_units.getTotalByteCount());
        curpos += this->units.getTotalByteCount();
    }

    // Input condition units
    if (curpos < reclen -1){
        CnField<string> unitsin(buf+curpos, en);
        units_in.set(unitsin.getData(), curpos, unitsin.getByteCount(), unitsin.getTotalByteCount());
        curpos += units_in.getTotalByteCount();
    }

    // result format
    if (curpos < reclen -1){
        CnField<string> cresfmt(buf+curpos, en);
        c_resfmt.set(cresfmt.getData(), curpos, cresfmt.getByteCount(), cresfmt.getTotalByteCount());
        curpos += c_resfmt.getTotalByteCount();
    }

    // low limit format
    if (curpos < reclen -1){
        CnField<string> cllmfmt(buf+curpos, en);
        c_llmfmt.set(cllmfmt.getData(), curpos, cllmfmt.getByteCount(), cllmfmt.getTotalByteCount());
        curpos += c_llmfmt.getTotalByteCount();
    }

    // High limit format
    if (curpos < reclen -1){
        CnField<string> chlmfmt(buf+curpos, en);
        c_hlmfmt.set(chlmfmt.getData(), curpos, chlmfmt.getByteCount(), chlmfmt.getTotalByteCount());
        curpos += c_hlmfmt.getTotalByteCount();
    }

    // High limit format
    if (curpos < reclen -1){
        NmField<float,4> lospec(buf+curpos, en);
        lo_spec.set(lospec.getData(), curpos, lospec.getByteCount(), lospec.getTotalByteCount());
        curpos += lo_spec.getTotalByteCount();
    }

    // High limit format
    if (curpos < reclen -1){
        NmField<float,4> hispec(buf+curpos, en);
        hi_spec.set(hispec.getData(), curpos, hispec.getByteCount(), hispec.getTotalByteCount());
    }
}

